# 什么叫异常?
# 你的程序出现中断,飘红,致使你的整个项目中断了.
# 错误类型:

# 语法错误.
# 语法错误就不应该现在你的代码中.
# if 1 > 2
#     pass
#
# # l1 = [11,22.44.556.788]
#
# while 1:
#     print(111)
#         print(222)

# 逻辑错误:
# num = input('请输入序号')
# int(num)
#
# l1 = [1, 2, 3]
# print(l1[4])

# dic = {1: 2,'a': 'b'}
# print(dic[3])
# KeyError

# 用if进行异常处理
# num = input('请输入:')
# if num.isdecimal():
#     num = int(num)
#     if 0 < num < 6 :
#         pass
#     elif num == 10:
#         pass
# else:
#     pass

# try

# 单分支

# try:
#     l1 = [1,2,3]
#     print(l1[100])
#
#     dic = {1: 2,'a': 'b'}
#     print(dic[3])
#
#     num = input('请输入序号')
#     int(num)
#
# except IndexError as e:
#     # print('处理了索引错误')
#     print(e)
#
# print(111)
# print(222)


# 多分支

# try:
#     # l1 = [1,2,3]
#     # print(l1[100])
#     #
#     # dic = {1: 2,'a': 'b'}
#     # print(dic[3])
#
#     num = input('请输入序号')
#     int(num)
#
# except IndexError as e:
#     print('处理了索引错误')
# except KeyError as e:
#     print('没有此键')
# except ValueError as e:
#     print('出现了转化错误')
#
# print(111)
# print(222)

# 及时解决异常,避免程序中断.
# 程序的分流.

dic = {
    1: 111,
    2: 333,
    3: 555,
}


# while 1:
#     try:
#         num = input('请输入序号')
#         int(num)
#         print(dic[int(num)])
#
#     except KeyError as e:
#         print('选项超出范围,请重新输入')
#     except ValueError as e:
#         print('请输入数字')

# print(111)
# print(222)

# 万能异常

# try:
#     num = input('请输入序号')
#     int(num)
#     print(dic[int(num)])
#
# except Exception as e:
#     print(e)
# print(111)

# 什么时候用万能异常,什么时候用多分支?
#如果你只是想把这个异常处理掉,让程序继续执行. 万能异常.
# 如果出现了异常,你是想根据不同的异常执行不同的逻辑流程,你要采取多分支.

# 第四种 万能 + 多分支

# dic = {
#     1: 111,
#     2: 333,
#     3: 555,
# }
#
#
# while 1:
#     try:
#         num = input('请输入序号')
#         int(num)
#         print(dic[int(num)])
#
#     except KeyError as e:
#         print('选项超出范围,请重新输入')
#     except ValueError as e:
#         print('请输入数字')
#     except Exception:
#         pass

# 异常处理其他成员

# try:
#     num = input('请输入序号')
#     int(num)
#     print(dic[int(num)])
#
# except KeyError as e:
#
#     print('选项超出范围,请重新输入')
# except Exception:
#     pass
# # else:  # 如果上面无异常执行else语句,否则不执行else语句.
# #     print(666)
#
# finally: # 在整个程序终止之前,执行finally
#     print('执行finally')

# 1. 文件操作
# f = open('register', mode='w')
# f.write('fdjsaklfd')
# f.write('fdjsaklfd')
# f.write('fdjsaklfd')
# l1 = [1,2,3]
# print(l1[1000])
# f.write('fdjsaklffjdsklaf')
#
# f.close()

# try:
#     f = open('register', mode='w')
#     f.write('fdjsaklfd')
#     f.write('fdjsaklfd')
#     f.write('fdjsaklfd')
#     l1 = [1, 2, 3]
#     print(111)
#     print(l1[1000])
#     f.write('fdjsaklffjdsklaf')
# finally:
#     print('哈哈哈哈')
    # f.close()

# 数据库: 连接数据库引擎.

# try:
#     int('e')
# finally:
#     print(777)

# 在函数中 finally.
# 结束函数之前,先要执行finally语句.

# def func():
#     try:
#         a = 1
#         b = 2
#         return a + b
#     finally:
#         print(666)
# func()

# 主动抛出异常:
# raise Exception('fdkslafjdslkf')

# 断言:
# assert 条件  # 源码上assert.
# assert 1 == 2
# print(11)
# print(22)
# print(33)


# 自定义异常(了解)

# TypeError
#
# class Connection(BaseException):
#
#     def __init__(self,msg):
#         self.msg = msg
#
#
# raise Connection('触发了连接异常')


# 异常处理总结:
# 异常处理不能经常使用:异常处理耗费性能.有些错误是需要进行分流使用.代码的可读性变差.
# 关键节点使用.


